<?php
namespace app\wechat\controller;
use think\Controller;
use think\Db;
use think\Request;
class User extends Controller
{
    protected $table_user_sms_info= 'user_sms_info';      /* 验证码信息表 */
    protected $table_user_bind_info= 'user_bind_info';    /* 绑定手机号 */
    protected $table_accounts_info= 'accounts_info';    /* 绑定手机号 */
    protected function Db_connect_user_sms_info(){
        $user_sms_info= Db::connect('db_qpplatformdb')->table($this->table_user_sms_info);
        return $user_sms_info;
    }
    protected function Db_connect_user_bind_info(){
        $user_bind_info= Db::connect('db_config2')->table($this->table_user_bind_info);
        return $user_bind_info;
    }
    protected function Db_connect_table_accounts_info(){
        $accounts_info= Db::connect('db_config2')->table($this->table_accounts_info);
        return $accounts_info;
    }
    public function index()
    {
        return  $this->fetch();
    }
    public function sendCode()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $code = rand(1000,9999);
            $data_info['Phone'] = $data['phone'];
            $data_info['Code'] = $code;
            $data_info['InsertTime'] = date('Y-m-d H:i:s',time());
            $data_info['ExpireTime'] = date('Y-m-d H:i:s',time()+15*60);
            $send = send_sms($data['phone'],1, ['code'=>$code]);
            if($send['Message'] != 'OK'){
                       file_put_contents('Aliyunsms_error_log.txt', '发送短信手机号: '.$data['phone'].' 。错误信息:'.$send['Message'].' 。错误码:'.$send['Code']."\r\n", FILE_APPEND);
                       return $this->error('发送失败');
            }else{
                    $res =$this->Db_connect_user_sms_info()->insert($data_info);
                    if($res){
                        return $this->success('发送成功');
                    }
            }
        }
    }
    public function check_phone()
    {
        if($this->request->isPost()){
              $data = $this->request->post();
              if($data['phone']==''){
                 return $this->error('手机号不能为空');
              }
              $sys_sms_arr =$this->Db_connect_user_sms_info()->field('Code,ExpireTime')->where('phone',$data['phone'])->order('InsertTime desc')->find();
              /* if(time()>=strtotime($sys_sms_arr['ExpireTime'])){
                  return $this->error('验证码已过期');
              }*/
              if($data['code']==$sys_sms_arr['Code']){
                     $bind_flag_arr =$this->Db_connect_user_bind_info()->field('UserID')->where('Phone',$data['phone'])->find();
                     if(!empty($bind_flag_arr)){
                          /* 存储用户ID */
                          session('UserID',$bind_flag_arr['UserID']);
                          /* 渠道区分 */
                          $SpreaderID=$this->Db_connect_table_accounts_info()->where('UserID',$bind_flag_arr['UserID'])->value('SpreaderID');
                          if($SpreaderID == 0 || $SpreaderID == 38){
                              $result['code'] = 1;
                              $result['msg'] = '官方';
                              return json($result);exit;
                          }else{
                              $result['code'] = 2;
                              $result['msg'] = '非官方';
                              return json($result);exit;
                          }
                     }else{
                         return  $this->error('账户绑定失败,请先在游戏内绑定该手机');
                     }
              }else{
                  return  $this->error('验证码错误');

              }
        }
    }
    public  function  Officialsetpass(){
          return  $this->fetch();
    }
    public  function  Unofficialsetpass(){
          return  $this->fetch();
    }
    public  function  setpass(){
        if($this->request->isPost()){
            $data = $this->request->post();
            dump(session('UserID'));
            $result=$this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->setField('LoginPass',$data['password']);
            if($result){
                $results['code'] = 1;
                $results['msg'] = '绑定成功';
                return json($results);exit;
            }else{
                $results['code'] = 0;
                $results['msg'] = '密码已使用';
                return json($results);exit;
            }
        }
    }
    public  function  congratulations(){
        $arr=$this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->find();
        $this->assign('arr',$arr);
        return  $this->fetch();
    }
    /* 重置密码 */
    public  function  resetpass(){
        return  $this->fetch();
    }
    public function repasset(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $LoginPass=$this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->value('LoginPass');
            if($LoginPass!=$data['oldpassword']){
                    $results['code'] = 0;
                    $results['msg'] = '原始密码不正确';
                    return json($results);exit;
            }
            $result=$this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->setField('LoginPass',$data['password']);
            if($result){
                $results['code'] = 1;
                $results['msg'] = '绑定成功';
                return json($results);exit;
            }else{
                $results['code'] = 0;
                $results['msg'] = '密码已使用';
                return json($results);exit;
            }
        }
        return  $this->fetch();
    }
    public function success_pass(){
          $arr=$this->Db_connect_table_accounts_info()->where('UserID',session('UserID'))->find();
          $this->assign('arr',$arr);
          return  $this->fetch();
    }


}
